package cn.yunhe.controller;

import cn.yunhe.model.Employee;
import cn.yunhe.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;


/**
 * Created by Administrator on 2017/7/22.
 */
@RestController
@SessionAttributes(names = "employee")
public class LoginController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/login")
    public ModelAndView login(){
        return new ModelAndView("login");
    }

    @RequestMapping("/doLogin")
    public ModelAndView doLogin(@RequestParam String username,@RequestParam String password){
        Employee e = new Employee();
        e.setUsername(username);
        e.setPassword(password);
        Employee employee = employeeService.searchEmployee(e);
        if (employee != null){
            ModelAndView mv = new ModelAndView("forward:/employee/searchAll");
            mv.addObject("employee",employee);
            return mv;
        }else {
            return new ModelAndView("login");
        }
    }



    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
}
